package com.lmx.repoDomain.repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lmx.common.util.CommonUtil;
import com.lmx.persistence.service.CustomerService;
import com.lmx.persistence.service.NursingService;
import com.lmx.repoDomain.tmodel.SpecService;

import io.katharsis.queryParams.QueryParams;
import io.katharsis.repository.annotations.JsonApiFindAll;
import io.katharsis.repository.annotations.JsonApiResourceRepository;

@JsonApiResourceRepository(SpecService.class)
@Component
public class SpecServiceRepository extends BaseRepository<SpecService> {

	@Autowired
	private NursingService nursingService;

	@Autowired
	private CustomerService customerService;

	@SuppressWarnings("unchecked")
	@Override
	@JsonApiFindAll
	public Iterable<SpecService> findAll(final QueryParams requestParams) {
		List<SpecService> listData = new ArrayList<>();
		Map<String, String> map = CommonUtil.getKeyMapByFilter(requestParams);
		String flagQueryType="";
		if(map.containsKey("flagQueryType")){
			flagQueryType=map.get("flagQueryType");
		}
		long queryStartDate = Long.parseLong(map.get("queryStartDate"));
		long queryEndDate = Long.parseLong(map.get("queryEndDate"));
		String positonType = map.get("positonType");
		String itemType = map.get("itemType");
		long uid;
		if (map.get("uid") != null && !"".equals(map.get("uid"))
				&& map.get("uid").matches("\\d+")) {
			uid = Long.parseLong(map.get("uid"));
			// 获取user对应护理组护理老人的服务项以及用药计划
			listData = nursingService.getNursingServiceByCustomer(null, uid,
					queryStartDate, queryEndDate, positonType, itemType, flagQueryType);
			// user id 获取当前登陆人的定时任务 岗位过滤
			// listData = customerService.getSpecServiceByUID(uid);
		}
		long cid;// customer id 获取当前登陆人所负责的特定老人的定时任务 岗位过滤
		if (map.get("cid") != null && !"".equals(map.get("cid"))
				&& map.get("cid").matches("\\d+")) {
			cid = Long.parseLong(map.get("cid"));
			listData = customerService.getSpecServiceByCID(cid);
		}
		long customerId;
		// 获取老人所有的服务项以及用药计划
		if (map.get("customerId") != null && !"".equals(map.get("customerId"))
				&& map.get("customerId").matches("\\d+")) {
			customerId = Long.parseLong(map.get("customerId"));
			listData = nursingService.getNursingServiceByCustomer(customerId,
					null, queryStartDate, queryEndDate, positonType, itemType, flagQueryType);
		} 
		return listData;
	}

}
